package com.stnet.web.enterprice.chinamobile;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.modules.commons.PageAction;
import com.opensymphony.xwork2.ActionContext;
import com.stnet.bases.Constants;
import com.stnet.entity.Transfer;
import com.stnet.entity.Users;

public class TransferAction extends PageAction
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Transfer> transferList;
    private Transfer transfer;
    private Long id;
    
    /**
	 * 用于提交update表单后，返回编辑前的页面
	 */
	private String _pageNo;
   
	
    public TransferAction()
    {
    	transfer = new Transfer();
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String execute()throws Exception{
      
    	super.pager = transferManagerDao.findTransfer(pageSize, pageNo, "-1");
        
        transferList = super.pager.getResultList();
        
        return Constants.SUCCESS_KEY;
    }

    /**
     * 查看一条友情链接信息
     * @return
     * @throws Exception
     */
    public String view()throws Exception{
        setTransfer(transferManagerDao.getTransfer(id));
        return Constants.VIEW_KEY;
    }

    /**
     * 查看一条友情链接信息，并跳转到添加页面
     * @return
     * @throws Exception
     */
    public String edit() throws Exception{
        setTransfer(transferManagerDao.getTransfer(id));
        return Constants.EDIT_KEY;
    }

    /**
     * 修改一条友情链接信息
     * @return
     * @throws Exception
     */
    public String update()throws Exception{
    	
    	Users users = null;
		Map session = (Map) ActionContext.getContext().getSession();
		if (session.get("users") != null)
			users = (Users) session.get("users");
		
		Transfer tran = this.transferManagerDao.getTransfer(transfer.getId());
		
		transfer.setEditedate(new Date());
		transfer.setUser(tran.getUser());
    	
    	transferManagerDao.updateTransfer(transfer);
    	
    	String _paga_No = _pageNo.equals("")||_pageNo==null?"1":_pageNo;
    	super.pager = transferManagerDao.findTransfer(pageSize, Integer.valueOf(_paga_No), "-1");
        transferList = super.pager.getResultList();
        return Constants.SUCCESS_KEY;
    }

    /**
     * 跳转到添加页面
     * @return
     * @throws Exception
     */
    public String add_page()throws Exception{
        return Constants.ADD_PAGE_KEY;
    }

    /**
     * 添加一条友情链接信息
     * @return
     * @throws Exception
     */
    public String add()throws Exception{
    	
    	Users users = null;
		Map session = (Map) ActionContext.getContext().getSession();
		if (session.get("users") != null)
			users = (Users) session.get("users");

		transfer.setEditedate(new Date());
		transfer.setUser(users);
		transfer.setId(null);
    	
    	transferManagerDao.addTransfer(transfer);
        return this.execute();
    }

    /**
     * 删除一条友情链接信息
     * @return
     * @throws Exception
     */
    public String delete()throws Exception{
        transferManagerDao.deleteTransfer(id);
        return this.execute();
    }

   

    public List<Transfer> getTransferList()
    {
        return transferList;
    }

    public void setTransferList(List<Transfer> transferList)
    {
        this.transferList = transferList;
    }

    public Transfer getTransfer()
    {
        return transfer;
    }

    public void setTransfer(Transfer transfer)
    {
        this.transfer = transfer;
    }

	public String get_pageNo() {
		return _pageNo;
	}

	public void set_pageNo(String no) {
		_pageNo = no;
	}

   
}